﻿using SimpleESBCore.Implement;
using SimpleESBCore.Implement.Datas;
using SimpleESBCore.Implement.Events;
using SimpleESBCore.Interfaces;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace SimpleESBClient.Handlers
{
    public class DataReceiveHandler : IHandler<EventBase>
    {
        ISerialization serialization;
        Dictionary<string, IHandler<DataSourceBase>> dataHandler;
        ILog log;
        public DataReceiveHandler(ISerialization serialization, Dictionary<string, IHandler<DataSourceBase>> dataHandler,ILog log)
        {
            this.serialization = serialization;
            this.dataHandler = dataHandler;
            this.log = log;
        }
        public void Handler(EventBase obj)
        {
            if (obj is ErrorEvent)
            {
                ErrorData errorData = obj.EventData as ErrorData;
                log.LogInfo(new LogInfoData(errorData.ErrorMessage));

            }
            else
            {
                string sourceType = (obj.EventData as DataSourceBase).Type;
                DataSourceBase dataObject = Activator.CreateInstance(sourceType.Split(',')[0], sourceType).Unwrap() as DataSourceBase;//创建一个原始数据得对象
                IHandler<DataSourceBase> handler = dataHandler[sourceType];
                handler.Handler(dataObject);
            }
        }
    }
}
